package com.mj.renminbi.activity;

import java.util.Timer;
import java.util.TimerTask;

import com.SYY.cbsdk.ui.AppControl;
import com.dyk.hfsdk.dao.util.DevListener;
import com.dyk.hfsdk.ui.Access;
import com.mj.renminbi.R;
import com.mj.renminbi.util.RMBconversion;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 人民币大写
 * @author zhaominglei
 * @date 2015-3-4
 * 
 */
public class MainActivity extends Activity implements OnClickListener,DevListener {
	@SuppressWarnings("unused")
	private static final String TAG = MainActivity.class.getSimpleName();
	private boolean isExit = false;
	private TimerTask timerTask;
	private LinearLayout miniAdLinearLayout; //迷你广告
	private RelativeLayout renminbiLayout;
	private TextView daxieText; //大写
	private ImageView copyView; //复制
	private EditText xiaoxieText; //小写
	private Button convertBtn; //转换
	private Button appOffersButton; //推荐应用
	private String result; //结果
	private Access access;
	private AppControl appControl;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main);
		init();
	}
	
	private void init() {
		miniAdLinearLayout = (LinearLayout)findViewById(R.id.miniAdLinearLayout);
		renminbiLayout = (RelativeLayout)findViewById(R.id.renminbi_relativelayout);
		daxieText = (TextView)findViewById(R.id.renminbi_daxie);
		copyView = (ImageView)findViewById(R.id.renminbi_copy);
		xiaoxieText = (EditText)findViewById(R.id.renminbi_xiaoxie_edt);
		convertBtn = (Button)findViewById(R.id.renminbi_convert_btn);
		appOffersButton = (Button)findViewById(R.id.appOffersButton);
		
		copyView.setOnClickListener(this);
		convertBtn.setOnClickListener(this);
		appOffersButton.setOnClickListener(this);
		
		appControl = AppControl.getInstance();
		appControl.init(MainActivity.this, "7cc7cbe157e88e9doOsJ+1GqiFHU3fax+dvceIdB42+LnJMSkHlQE6tUk5nqnyDWtw", "木蚂蚁");
		appControl.loadPopAd(MainActivity.this);
		appControl.showPopAd(MainActivity.this, 60 * 1000);
		appControl.showInter(MainActivity.this, miniAdLinearLayout);
		
		access = Access.getInstance();
		// 初始化带player_id
		access.init(MainActivity.this, "7cc7cbe157e88e9doOsJ+1GqiFHU3fax+dvceIdB42+LnJMSkHlQE6tUk5nqnyDWtw", "木蚂蚁");
		// 设置初始积分                                                              
		access.setdefaultSCORE(this, 100);
		// 设置获取积分的监听
		access.setAppListener(this, this);	
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.renminbi_copy:
			if (result != null && !result.equals("")) {
				ClipboardManager clipboardManager = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
				clipboardManager.setText(result);
				Toast.makeText(getApplicationContext(), R.string.renminbi_copy_success, Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.renminbi_convert_btn:
			String xiaoxie = xiaoxieText.getText().toString();
			if (xiaoxie == null || xiaoxie.equals("")) {
				Toast.makeText(getApplicationContext(), R.string.renminbi_xiaoxie_hint, Toast.LENGTH_SHORT).show();
				return;
			}
			if (xiaoxie.indexOf(".") == (xiaoxie.length()-1)) {
				Toast.makeText(getApplicationContext(), R.string.renminbi_xiaoxie_error1, Toast.LENGTH_SHORT).show();
				return;
			}
			String currencyDigits = xiaoxie.replaceAll("\\.", "");
			for (int i = 0; i < currencyDigits.length(); i++) {
				if (!Character.isDigit(currencyDigits.charAt(i))) {
					Toast.makeText(getApplicationContext(), R.string.renminbi_xiaoxie_error1, Toast.LENGTH_SHORT).show();
					return;
				}
			}
			if(Double.parseDouble(xiaoxie) > RMBconversion.MAXIMUM_NUMBER) {
				Toast.makeText(getApplicationContext(), R.string.renminbi_xiaoxie_error2, Toast.LENGTH_SHORT).show();
				return;
			}
			result = new RMBconversion().conversion2(xiaoxie);
			if (result != null && !result.equals("")) {
				daxieText.setText(result);
				renminbiLayout.setVisibility(View.VISIBLE);
			}
			break;
			
		case R.id.appOffersButton:
			access.openWALL(MainActivity.this);
			break;

		default:
			break;
		}
	}
	
	@Override
	public void onBackPressed() {
		if (isExit) {
			MainActivity.this.finish();
		} else {
			isExit = true;
			Toast.makeText(MainActivity.this, R.string.exit_msg, Toast.LENGTH_SHORT).show();
			timerTask = new TimerTask() {
				@Override
				public void run() {
					isExit = false;
				}
			};
			new Timer().schedule(timerTask, 2*1000);
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		appControl.close(this);
	}
	
	@Override
	public void onDevSucceed(int eggs) {
		System.out.println("积分获取成功:" + eggs);
	}

	@Override
	public void onDevFailed(String message) {
		System.out.println("积分获取失败:" + message);
	}

	@Override
	public void onClose(Context context) {
		((Activity) context).finish();
	}
}
